import com.mochimedia.ui.ColorTools;
import TextField.StyleSheet;

class com.mochimedia.ui.Style {
    
    private var _styleSheet:TextField.StyleSheet;
    
    private var css:String;

    private var _textColor:Number = 0x333333;
    private var _titleColor:Number = 0x003399;
    private var _linkColor:Number = 0x0066cc;
    private var _hoverColor:Number = 0x3388ff;
    private var _inverseTextColor:Number = 0xffffff;
    private var _backgroundColor:Number = 0xffffff;
    private var _backgroundAlpha:Number = 100;
    private var _buttonColor:Number = 0x3399ff;
    private var _borderColor:Number;
    private var _unselectedColor:Number;
    private var _unselectedTextColor:Number;
    private var _inactiveColor:Number = 0x999999;
    private var _inactiveTextColor:Number;
    private var _haloColor:Number = 0xff9900;
	private var _highlightTextColor:Number = 0xcc6600;
    
    private var _gradient:Boolean = true;
    private var _background:Boolean = true;
    private var _border:Boolean = true;
	
	private var _bgGradient:Boolean = false;
	private var _bgGradientColors:Array = [0xffffff, 0xeeeeee];
	private var _bgGradientRatios:Array = [0, 255];
	private var _bgGradientHeight:Number = 0;
    
    private var _padding:Number = 6;
    private var _round:Number = 8;
    private var _borderWidth:Number = 4;
    
    private var _fontSize:Number = 12;
    private var _titleFontSize:Number = 15;
    
    private var _embedFonts:Boolean = false;
    private var _font:String = "Verdana";
    private var _fonts:Array = ["Verdana", "Tahoma", "Arial"];
    private var _htmlFont:String = "Verdana, Helvetica, Arial";
    
    public function get textColor():Number { return _textColor; }
    public function set textColor(value:Number):Void { 
        _textColor = value; 
        _inverseTextColor = ColorTools.getInverseColor(_textColor);
        updateColors();
        updateCSS();
    }
    
    public function get titleColor():Number { return _titleColor; }
    public function set titleColor(value:Number):Void { 
        _titleColor = value;
        updateColors();
        updateCSS();
    }
    
    public function get linkColor():Number { return _linkColor; }
    public function set linkColor(value:Number):Void {
        _linkColor = value;
        updateColors();
        updateCSS();
    }

    public function get hoverColor():Number { return _hoverColor; }
    public function set hoverColor(value:Number):Void {
        _hoverColor = value;
        updateCSS();
    }
    
    public function get backgroundColor():Number { return _backgroundColor; }
    public function set backgroundColor(value:Number):Void {
        _backgroundColor = value;
        updateColors();
        updateCSS();
    }
    
    public function get buttonColor():Number { return _buttonColor; }
    public function set buttonColor(value:Number):Void { 
        _buttonColor = value;
        updateColors();
    }
    
    public function get borderColor():Number { return (!isNaN(_borderColor)) ? _borderColor : _textColor; }
    public function set borderColor(value:Number):Void { 
        _borderColor = value;
        updateColors();
    }
    
    public function get inverseTextColor():Number { return _inverseTextColor; } 
    public function set inverseTextColor(value:Number):Void { _inverseTextColor = value; }
    
    public function get gradient():Boolean { return _gradient; }
    public function set gradient(value:Boolean):Void { _gradient = value; }
    
    public function get background():Boolean { return _background; }
    public function set background(value:Boolean):Void { _background = value; }
    
    public function get backgroundAlpha():Number { return _backgroundAlpha; }
    public function set backgroundAlpha(value:Number):Void { _backgroundAlpha = value; }    
    
    public function get border():Boolean { return _border; }
    public function set border(value:Boolean):Void { _border = value; }
    
    public function get padding():Number { return _padding; }
    public function set padding(value:Number):Void { _padding = value; }
    
    public function get round():Number { return _round; }
    public function set round(value:Number):Void { _round = value; }
    
    public function get borderWidth():Number { return _borderWidth; }
    public function set borderWidth(value:Number):Void { _borderWidth = value;  }
    
    public function get fontSize():Number { return _fontSize; }
    public function set fontSize(value:Number):Void { 
        _fontSize = value;
        updateCSS();
    }
    
    public function get titleFontSize():Number { return _titleFontSize; }
    public function set titleFontSize(value:Number):Void { _titleFontSize = value; }
    
    public function get inactiveColor():Number { return _inactiveColor; }
    public function set inactiveColor(value:Number):Void { _inactiveColor = value; }
    
    public function get inactiveTextColor():Number { return _inactiveTextColor; }
    public function set inactiveTextColor(value:Number):Void { _inactiveTextColor = value; }
    
    public function get unselectedColor():Number { return _unselectedColor; }
    public function set unselectedColor(value:Number):Void { _unselectedColor = value; }
    
    public function get unselectedTextColor():Number { return _unselectedTextColor; }
    public function set unselectedTextColor(value:Number):Void { _unselectedTextColor = value; }
    
    public function get styleSheet():StyleSheet { return _styleSheet; }
    public function set styleSheet(value:StyleSheet):Void { _styleSheet = value; }
    
    public function get haloColor():Number { return _haloColor; }
    public function set haloColor(value:Number):Void { _haloColor = value; }
    
    public function get embedFonts():Boolean { return _embedFonts; }
    public function set embedFonts(value:Boolean):Void { _embedFonts = value; }
    
    public function get font():String { return _font; }
    public function set font(value:String):Void { _font = value; }
    
    public function setFont (idx:Number):Void { if (_fonts[idx] != null) _font = _fonts[idx]; }
    
    public function getFontsAsString ():String { return _font + ", " + _fonts.join(", "); }
    
    public function get htmlFont():String { return _htmlFont; }
    public function set htmlFont(value:String):Void { 
        _htmlFont = value;
        updateCSS();
    }
	
	public function get highlightTextColor():Number { return _highlightTextColor; }
	public function set highlightTextColor(value:Number):Void { _highlightTextColor = value; }
	
	public function get bgGradient():Boolean { return _bgGradient; }
	public function set bgGradient(value:Boolean):Void { _bgGradient = value; }
	
	public function get bgGradientColors():Array { return _bgGradientColors; }
	public function set bgGradientColors(value:Array):Void { _bgGradientColors = value; }
	
	public function get bgGradientRatios():Array { return _bgGradientRatios; }
	public function set bgGradientRatios(value:Array):Void { _bgGradientRatios = value; }
	
	public function get bgGradientHeight():Number { return _bgGradientHeight; }
	public function set bgGradientHeight(value:Number):Void { _bgGradientHeight = value; }
     
    private var _options:Object;
    
    
    //
    //
    public function Style (options:Object) {
        
        init(options);
        
    }
    
    //
    //
    private function init (options:Object):Void {
        
        _options = options;
        
        for (var param:String in options) if (this[param] != undefined) this[param] = options[param] else debug ("WARNING: " + param + " does not exist in ui.Style");
        
        _haloColor = ColorTools.getTintedColor(ColorTools.getInverseColor(_buttonColor), _backgroundColor, 0.2);
        _inactiveTextColor = ColorTools.getTintedColor(_inactiveColor, 0x000000, 0.3);
        _unselectedTextColor = ColorTools.getTintedColor(_inverseTextColor, _buttonColor, 0.3);
        _unselectedColor = ColorTools.getTintedColor(_buttonColor, _borderColor, 0.25);
        
        if (_styleSheet == undefined) _styleSheet = new TextField.StyleSheet();
        
        updateCSS();
       
        if (System.capabilities.os.indexOf("Mac") == -1) {
           
            _fonts = ["Verdana", "Arial", "Tahoma"];
            
        } else {
            
            _fonts = ["Verdana", "Monaco", "Geneva"];
            
        }
        
    }
    
    //
    //
    private function updateColors ():Void {

        _haloColor = ColorTools.getTintedColor(ColorTools.getInverseColor(_buttonColor), _backgroundColor, 0.2);
        _inactiveTextColor = ColorTools.getTintedColor(_inactiveColor, 0x000000, 0.3);
        _unselectedTextColor = ColorTools.getTintedColor(_inverseTextColor, _buttonColor, 0.3);
        _unselectedColor = ColorTools.getTintedColor(_buttonColor, _borderColor, 0.25);
		_highlightTextColor = ColorTools.getTintedColor(ColorTools.getInverseColor(_linkColor), ColorTools.getInverseColor(_backgroundColor), 0.2);
        _hoverColor = ColorTools.getTintedColor(_linkColor, 0xffffff, 0.3);
        
    }
    
    
    //
    //
    private function updateCSS ():Void {
        
        var tColor:String = ColorTools.numberToHTMLColor(_titleColor);
        var txtColor:String = ColorTools.numberToHTMLColor(_textColor);
        var lColor:String = ColorTools.numberToHTMLColor(_linkColor);
        var hColor:String = ColorTools.numberToHTMLColor(_hoverColor);
        var nColor:String = ColorTools.numberToHTMLColor(ColorTools.getTintedColor(textColor, backgroundColor, 0.5));
        
        css = "";
        
        css += "h1 { font-family: " + _htmlFont + "; font-weight: bold; font-size: " + Math.min(18, fontSize * 2) + "px; color: " + tColor + "; leading: 10px; } ";      
        css += "h2 { font-family: " + _htmlFont + "; font-weight: bold; font-size: " + (fontSize + 6) + "px; color: " + tColor + "; leading: 8px; } ";       
        css += "h3 { font-family: " + _htmlFont + "; font-weight: bold; font-size: " + (fontSize + 3) + "px; color: " + tColor + "; leading: 6px; } ";
        css += "h4 { font-family: " + _htmlFont + "; font-weight: bold; font-size: " + (fontSize + 1) + "px; color: " + tColor + "; leading: 4px; } ";
        css += "p { font-family: " + _htmlFont + "; font-weight: normal; font-size: " + fontSize + "px; color: " + txtColor + "; leading: 2px; } ";
        css += "a { font-weight: bold; color: " + lColor + "; text-decoration: underline; } ";
        css += "a:hover { font-weight: bold; color: " + hColor + "; textDecoration: underline; } ";
        
        css += ".note { font-family: Monaco, " + _htmlFont + "; font-weight: normal; font-size: " + Math.max(12, _fontSize - 2) + "px; color: " + nColor + "; leading: 0px; } ";
        css += ".numeric { font-family: Monaco, Lucida Sans Unicode," + _htmlFont + "; font-weight: normal; font-size: " + Math.max(12, _fontSize - 2) + "px; color: " + txtColor + "; leading: 2px; } ";
        
        if (!_styleSheet.parseCSS(css)) debug("CSS error!");
        
    }
    
    //
    //
    public function clone (overrides:Object):Style {
        
        var optCopy = { };
        
        for (var param:String in _options) optCopy[param] = _options[param];
        for (var param:String in this) {
            if (param.indexOf("_") == 0) {
                if (this[param] instanceof Array) optCopy[param] = Array(this[param]).concat();
				else optCopy[param] = this[param];
            }
        }
        for (var param:String in overrides) optCopy[param] = overrides[param];
        for (var param:String in overrides) optCopy["_" + param] = overrides[param];
        return new Style(optCopy);
        
    }
    
    //
    //
    private function debug (msg:String):Void {
        
        //trace(msg);
        
    }
    
}
